package com.nut.service.impl;

import com.nut.entity.User;
import com.nut.mapper.UserMapper;
import com.nut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-04-15 15:57
 */

@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserMapper userMapper;

    /**
     * @Description 查询所有User
     * @return 所有User的List集合
     **/
    @Override
    public List<User> findAll()
    {
        return userMapper.findAll();
    }

    /**
     * @Description 添加User
     * @param user:前台传递的User
     **/
    @Override
    public void saveUser(User user)
    {
        userMapper.add(user);
    }

    /**
     * @Description 校验name是否重复
     * @param name: 提交的name
     * @return boolean
     **/
    @Override
    public boolean checkUserByName(String name)
    {
        User user = userMapper.queryUserByName(name);
        return user != null;
    }

    /**
     * @Description 根据id获取User
     * @param number：id
     * @return User
     **/
    @Override
    public User getUserById(int number)
    {
        return userMapper.queryUserById(number);
    }

    /**
     * @Description 修改User
     * @param user: 前台校验后的User
     * @return void
     **/
    @Override
    public void updateUser(User user)
    {
        userMapper.update(user);
    }

    /**
     * @Description 根据id删除User
     * @param number: 前台传递的id
     * @return void
     **/
    @Override
    public void deleteUserById(int number)
    {
        userMapper.delete(number);
    }

    /**
     * @Description 批量删除User
     * @param numbers: 前台传递的ids
     * @return void
     **/
    @Override
    public void batchDelete(List<Integer> numbers)
    {
        userMapper.batchDelete(numbers);
    }
    
    /**
     * @Description User登录
     * @param user: 前台传递user
     * @return 查询到的User
     **/
    @Override
    public User login(User user)
    {
        return userMapper.query(user);
    }

    /**
     * @Description 转账
     * @param userA: 转出
     * @param userB: 转入
     * @return void
     **/
    @Override
    //@Transactional
    public void transfer(User userA, User userB)
    {
        //userA扣钱
        userMapper.update(userA);

        //异常？？？？？
        //int i = 1/0;

        //userB加钱
        userMapper.update(userB);
    }
}
